package com.tuanzi.loan.web.bean.family.sms;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.sms.SmsSendTask;
import com.tuanzi.loan.business.entity.sms.SmsVoiceSendTask;
import com.tuanzi.loan.business.service.sms.SmsSendTaskService;
import com.tuanzi.loan.business.service.sms.SmsVoiceSendTaskService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.family.SmsSendTaskSearchRequest;
import com.tuanzi.loan.vo.request.family.sms.SmsVoiceSendTaskSearchRequest;
import com.tuanzi.loan.vo.response.family.SmsSendTaskSearchResponse;
import com.tuanzi.loan.vo.response.family.sms.SmsVoiceSendTaskSearchResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.family.SmsSendTaskLazyDataModel;
import com.tuanzi.loan.web.lazy.family.sms.SmsVoiceSendTaskLazyDataModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.List;

import static com.tuanzi.loan.business.constants.DictionaryCode.*;

/**
 * @Author: chenweijun
 * @Date: 2018/10/22 19:44
 * @Description:
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class SmsVoiceSendTaskBean extends JSFListingBean<SmsVoiceSendTaskSearchRequest, SmsVoiceSendTaskSearchResponse> {


    @Autowired
    private SmsVoiceSendTaskService smsVoiceSendTaskService;

    private String taskErrorReason;
    private String smsPlatformResult;

    private SmsVoiceSendTask smsVoiceSendTask;

    @PostConstruct
    private void init() {
        initLazyDataModel();
    }

    public void onReset() {
        initLazyDataModel();
    }

    private void initLazyDataModel() {
        $ = new SmsVoiceSendTaskSearchRequest();
        lazyDataModel = new SmsVoiceSendTaskLazyDataModel($);
    }

    @Override
    protected List<String> getDictionaryCodes() {
        return Lists.newArrayList(SMS_VOICE_SEND_STATUS, SMS_VOICE_PLATFORM_CHANNEL, SMS_VOICE_ID);
    }

    public String onViewCondition(Long id) {
        return redirect("/views/family/user-info/send-sms/sms-voice-condition-view.xhtml?sendVoiceTaskId=" + id);
    }

    public void onViewError(Long id) {
        SmsVoiceSendTask smsVoiceSendTask = smsVoiceSendTaskService.findOne(id);
        taskErrorReason = smsVoiceSendTask.getErrorReason();
    }

    public void onViewResult(Long id) {
        SmsVoiceSendTask smsVoiceSendTask = smsVoiceSendTaskService.findOne(id);
        smsPlatformResult = smsVoiceSendTask.getResult();
    }

    public void onCancel(Long smsVoiceSendTaskId) {
        smsVoiceSendTaskService.cancelTask(smsVoiceSendTaskId);
        addSuccessfullyMessage();
    }

    public void onResend(Long smsSendTaskId) {
        smsVoiceSendTaskService.resend(smsSendTaskId);
        addSuccessfullyMessage();
    }

    public void onSelect(Long id) {
        smsVoiceSendTask = smsVoiceSendTaskService.findOne(id);
    }

    public void onEdit() {
        smsVoiceSendTaskService.saveOrUpdate(smsVoiceSendTask);
    }
}
